//
//  ISocketProtocol.h
//  ZYWebImage
//
//  Created by wangzhipan on 2025/4/25.
//

#ifndef ISocketProtocol_h
#define ISocketProtocol_h

typedef NS_ENUM(NSInteger, ISocketErrorCode) {
    ISocketErrorCodeUnknow = 10009991,
    ISocketErrorCodeCreateFaild,
    ISocketErrorCodeHostResolveFaild,  //主机名解析失败
    ISocketErrorCodeConnectFaild,
    ISocketErrorCodeNotConnect,
    ISocketErrorCodeSendFaild,
    ISocketErrorCodeReceiveFild,
    ISocketErrorCodeConnectClosed,
    ISocketErrorCodeSendTimeout
};

@protocol ISocketProtocol;
@protocol ISocketDelegate <NSObject>
@optional
- (void)socketDidConnect:(nullable id <ISocketProtocol>)socketManager;
- (void)socket:(nullable id <ISocketProtocol>)socket didDisconnectWithError:(nullable NSError *)error;
- (void)socket:(nullable id <ISocketProtocol>)socket didReceiveData:(nullable NSData *)data;
- (void)socket:(nullable id <ISocketProtocol>)socket didSendData:(nullable NSData *)data;
- (void)socket:(nullable id <ISocketProtocol>)socket didFailError:(nullable NSError *)error;
@end

@protocol ISocketProtocol <NSObject>
@optional
@property (nonatomic, weak, nullable) id<ISocketDelegate> delegate;
- (instancetype)initWithHost:(NSString *)host port:(NSUInteger)port;

// 连接方法
- (BOOL)connect:(NSError * _Nullable *)error;
- (void)asyncConnect;

// 断开连接
- (void)disconnect;

// 发送数据
- (BOOL)sendData:(NSData *)data error:(NSError * _Nullable *)error;
- (BOOL)sendData:(NSData *)data timeout:(NSTimeInterval)timeout error:(NSError * _Nullable *)error;
- (void)asyncSendData:(NSData *)data;
- (void)asyncSendData:(NSData *)data timeout:(NSTimeInterval)timeout completion:(void (^)(NSError *error))completion;

// 读取数据
- (void)readDataToLength:(NSUInteger)length timeout:(NSTimeInterval)timeout completion:(void (^)(NSData *data, NSError *error))completion;
- (void)readDataToData:(NSData *)data timeout:(NSTimeInterval)timeout completion:(void (^)(NSData *data, NSError *error))completion;

// 心跳机制
- (void)startHeartbeatWithData:(NSData *)data interval:(NSTimeInterval)interval;
- (void)stopHeartbeat;
@end

static NSString * const kSocketOriginDataKey = @"kSocketOriginDataKey";

#endif /* ISocketProtocol_h */
